package com.mosai.alignpro.util;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;

import com.alibaba.fastjson.JSON;
import com.mosai.alignpro.CameraActivity;
import com.mosai.alignpro.DiagnosisListActivity;
import com.mosai.alignpro.entity.DoctorInfo;
import com.mosai.alignpro.entity.ImageInfo;
import com.mosai.alignpro.entity.PatientInfo;

import java.util.HashMap;

/**
 * Created by Rays on 2020/3/31.
 */
@SuppressWarnings("WeakerAccess")
public final class IntentUtils {
    public static final String TAG = IntentUtils.class.getSimpleName();

    public static final String ACTION_RN_ACTIVITY = "com.alignpro.rnactivity";
    public static final String EXTRA_PATIENT_INFO = "patientInfo";
    public static final String EXTRA_NEW_RN = "newRN";
    public static final String ACTION_PATIENT_DETAIL = "patientDetail";
    public static final String ACTION_DOCTOR_DETAIL = "doctorDetail";
    public static final String ACTION_BIND_PATIENT_IMAGE = "selectPatient";
    public static final String ACTION_SELECT_DOCTOR = "selectDoctor";
    public static final String NEW_RN_ACTION = "action";
    public static final String NEW_RN_DATA = "data";

    private static boolean isPatientApp;

    public static void goPatientDetail(Activity activity, PatientInfo info) {
        goNewRN(activity, ACTION_PATIENT_DETAIL, info);
    }

    public static void goDoctorDetail(Activity activity, DoctorInfo doctorInfo) {
        goNewRN(activity, ACTION_DOCTOR_DETAIL, doctorInfo);
    }

    public static void goSelectDoctor(Activity activity, ImageInfo imageInfo) {
        goNewRN(activity, ACTION_SELECT_DOCTOR, imageInfo);
    }

    public static void goBindPatientImage(Activity activity, ImageInfo imageInfo) {
        goNewRN(activity, ACTION_BIND_PATIENT_IMAGE, imageInfo);
    }

    private static void goNewRN(Activity activity, String action, Object data) {
        Intent intent = new Intent(ACTION_RN_ACTIVITY);
        HashMap<String, Object> map = new HashMap<>(2, 1.0f);
        map.put(NEW_RN_ACTION, action);
        map.put(NEW_RN_DATA, data);
        String value = JSON.toJSONString(map);
        LogUtils.i(TAG, "newRN: " + value);
        intent.putExtra(EXTRA_NEW_RN, value);
        intent.setData(Uri.parse("alignpro://" + activity.getPackageName()));
        if (intent.resolveActivity(activity.getPackageManager()) != null) {
            activity.startActivity(intent);
        }
    }

    public static void goDiagnosisList(Activity activity, int requestCode) {
        goDiagnosisList(activity, requestCode, null);
    }

    public static void goDiagnosisList(Activity activity, int requestCode, String data) {
        Intent intent = new Intent(activity, DiagnosisListActivity.class);
        intent.putExtra(DiagnosisListActivity.EXTRA_DATA, data);
        activity.startActivityForResult(intent, requestCode);
    }

    public static void goDiagnosisListByPatientDetail(Activity activity, int requestCode, String data) {
        goDiagnosisList(activity, requestCode, data);
    }

    public static void goCamera(Activity activity, int requestCode, String param) {
        CameraActivity.startForResult(activity, requestCode, param);
    }

    public static void setPatientApp() {
        isPatientApp = true;
    }

    public static void setDoctorApp() {
        isPatientApp = false;
    }

    public static boolean isPatientApp() {
        return isPatientApp;
    }

    public static boolean isDoctorApp() {
        return !isPatientApp;
    }
}
